const { request } = require('express')
const express = require('express')
const app = express()
app.get('/server', (request, response) => {
    //设置允许跨域 全部允许
    response.setHeader('Access-Control-Allow-Origin', '*')
    response.send({ a: 100, b: 200 })
})
app.get('/server2', (request, response) => {
    //设置跨域 单独指定域名可以请求
    response.setHeader('Access-Control-Allow-Origin', 'http://192.168.2.115:2000')
    response.send({ a: 100, b: 200 })
})
/*
jsonp 跨域原理
    Img script不受同源策略影响
    JSONP 请求数据，服务端要返回一个可执行的js代码
*/
/*只能通过get方式*/
app.get('/server3', (request, response) => {
    let callback = request.query.callback
    let _d = callback + "({'a':'100'})"
    response.send(_d)
})

/*代理*/
app.get('/server4', (request, response) => {
    response.send({ a: 100 })
})

app.get('/server5', (request, response) => {
    response.send({ a: 100 })
})

app.listen(3000, () => {
    console.log('启动')
})